home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue52 / Clinic / TimeZoneU.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-11-01  |  4.1 KB  |  130 lines

  1. unit TimeZoneU;
  2.  
  3. {$ifdef Ver90} { Delphi 2.0x }
  4.   {$define DelphiLessThan3}
  5.   {$define DelphiLessThan4}
  6. {$endif}
  7. {$ifdef Ver93} { C++ Builder 1.0x }
  8.   {$define DelphiLessThan3}
  9.   {$define DelphiLessThan4}
  10. {$endif}
  11. {$ifdef Ver100} { Delphi 3.0x }
  12.   {$define DelphiLessThan4}
  13. {$endif}
  14. {$ifdef Ver110} { C++ Builder 3.0x }
  15.   {$define DelphiLessThan4}
  16. {$endif}
  17.  
  18. interface
  19.  
  20. uses
  21.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  22.   StdCtrls;
  23.  
  24. type
  25.   TTimeZoneInfoForm = class(TForm)
  26.     lblCurrent: TLabel;
  27.     lblBias: TLabel;
  28.     lblStdBias: TLabel;
  29.     lblDayBias: TLabel;
  30.     lblDayToStd: TLabel;
  31.     lblStdToDay: TLabel;
  32.     procedure FormCreate(Sender: TObject);
  33.   private
  34.     { Private declarations }
  35.   public
  36.     { Public declarations }
  37.   end;
  38.  
  39. var
  40.   TimeZoneInfoForm: TTimeZoneInfoForm;
  41.  
  42. implementation
  43.  
  44. {$R *.DFM}
  45.  
  46. {$ifdef DelphiLessThan3}
  47. type
  48.   EWin32Error = class(Exception);
  49.  
  50. procedure RaiseLastWin32Error;
  51. var
  52.   LastError: DWord;
  53. begin
  54.   LastError := GetLastError;
  55.   if LastError <> ERROR_SUCCESS then
  56.     raise EWin32Error.Create(SysErrorMessage(LastError))
  57. end;
  58.  
  59. function SystemTimeToDateTime(const SystemTime: TSystemTime): TDateTime;
  60. begin
  61.   with SystemTime do
  62.     Result := EncodeDate(wYear, wMonth, wDay) +
  63.       EncodeTime(wHour, wMinute, wSecond, wMilliSeconds);
  64. end;
  65. {$endif}
  66.  
  67. procedure TTimeZoneInfoForm.FormCreate(Sender: TObject);
  68. var
  69.   RetVal: DWord;
  70.   TZI: TTimeZoneInformation;
  71.   StdBias, DayBias: Integer;
  72.   StdName, DayName: String;
  73. const
  74.   OrdNums: array[1..5] of String = ('1st', '2nd', '3rd', '4th', 'last');
  75.   MinsPerDay = SecsPerDay / 60;
  76. {$ifdef DelphiLessThan4}
  77.   TIME_ZONE_ID_UNKNOWN  = 0;
  78.   TIME_ZONE_ID_STANDARD = 1;
  79.   TIME_ZONE_ID_DAYLIGHT = 2;
  80. {$endif}
  81. begin
  82.   RetVal := GetTimeZoneInformation(TZI);
  83.   if RetVal = $FFFFFFFF then //API call failed
  84.     RaiseLastWin32Error;
  85.   if TZI.StandardName[0] = #0 then //No name information
  86.     StdName := 'standard time'
  87.   else
  88.     StdName := WideCharToString(TZI.StandardName);
  89.   if TZI.DaylightName[0] = #0 then //No name information
  90.     DayName := 'daylight time'
  91.   else
  92.     DayName := WideCharToString(TZI.DaylightName);
  93.   case RetVal of
  94.     TIME_ZONE_ID_UNKNOWN: lblCurrent.Caption := Format(lblCurrent.Caption, ['unknown time frame']);
  95.     TIME_ZONE_ID_STANDARD: lblCurrent.Caption := Format(lblCurrent.Caption, [StdName]);
  96.     TIME_ZONE_ID_DAYLIGHT: lblCurrent.Caption := Format(lblCurrent.Caption, [DayName]);
  97.   end;
  98.   lblBias.Caption := Format(lblBias.Caption, [TZI.Bias]);
  99.   StdBias := TZI.Bias + TZI.StandardBias;
  100.   lblStdBias.Caption := Format(lblStdBias.Caption, [StdBias, StdName]);
  101.   DayBias := TZI.Bias + TZI.DaylightBias;
  102.   lblDayBias.Caption := Format(lblDayBias.Caption, [DayBias, DayName]);
  103.   lblDayToStd.Caption := Format(lblDayToStd.Caption, [DayName, StdName]);
  104.   lblStdToDay.Caption := Format(lblStdToDay.Caption, [StdName, DayName]);
  105.   if TZI.StandardDate.wMonth = 0 then
  106.   begin
  107.     lblDayToStd.Caption := lblDayToStd.Caption + 'an unspecified point';
  108.     lblStdToDay.Caption := lblStdToDay.Caption + 'an unspecified point';
  109.     Exit;
  110.   end;
  111.   if TZI.StandardDate.wYear = 0 then //"Day of month" date
  112.     with TZI.StandardDate do
  113.       lblDayToStd.Caption := Format('%s%s on the %s %s of %s', [lblDayToStd.Caption,
  114.         TimeToStr(EncodeTime(wHour, wMinute, wSecond, wMilliseconds) + DayBias / MinsPerDay),
  115.         OrdNums[wDay], LongDayNames[wDayOfWeek + 1], LongMonthNames[wMonth]])
  116.   else //Absolute date
  117.     lblDayToStd.Caption := lblDayToStd.Caption +
  118.       DateTimeToStr(SystemTimeToDateTime(TZI.StandardDate) + DayBias / MinsPerDay);
  119.   if TZI.DaylightDate.wYear = 0 then //"Day of month" date
  120.     with TZI.DaylightDate do
  121.       lblStdToDay.Caption := Format('%s%s on the %s %s of %s', [lblStdToDay.Caption,
  122.         TimeToStr(EncodeTime(wHour, wMinute, wSecond, wMilliseconds) + StdBias / MinsPerDay),
  123.         OrdNums[wDay], LongDayNames[wDayOfWeek + 1], LongMonthNames[wMonth]])
  124.   else //Absolute date
  125.     lblStdToDay.Caption := lblStdToDay.Caption +
  126.       DateTimeToStr(SystemTimeToDateTime(TZI.DaylightDate) + StdBias / MinsPerDay)
  127. end;
  128.  
  129. end.
  130.